#!/bin/bash

init_params() {
  work_dir=benchmark_data
  script_dir=${work_dir}/benchmark_script
  result_all_dir=${work_dir}/benchmark_result
  device_dir=/mesh_services
  device_work_dir=${device_dir}/${work_dir}
  device_script_dir=${device_dir}/${script_dir}
}

log() {
  echo "$(date '+%Y-%m-%d %H:%M:%S')  $*"
}

build() {
  npm run build
}
device_init() {
  adb -s "$1" wait-for-device root
  sleep 1s
  adb -s "$1" wait-for-device remount
}
# 提前把脚本放到 script_dir
push() {
  mkdir -p ./${script_dir}
  cp -r src/device_benchmark_test.sh ./${script_dir}
  cp -r monitor ./${script_dir}
  adb -s "$1" shell mkdir -p ${device_script_dir}
  adb -s "$1" push ./${script_dir}/* ${device_script_dir}
}
pull_result() {
  mkdir -p ./${result_dir}
  adb -s "$1" pull ${device_result_dir} ./${result_dir}
}
clean() {
  rm -rf ./${work_dir}
  adb -s "$1" shell "rm -rf ${device_work_dir}"
}
benchmark_init() {
  benchmark_time=$(echo "$*" | egrep -o "benchmark_time=[^ ]+" | cut -d= -f 2)
  if [[ "$benchmark_time" == "" ]]; then
    benchmark_time=$(date +%Y_%m_%d_%H_%M_%S)
  fi
  result_dir=${result_all_dir}/${benchmark_time}
  device_result_dir=${device_dir}/${result_dir}
  test_info="device_result_dir=${device_result_dir} device_script_dir=${device_script_dir} device_work_dir=${device_work_dir}"
  adb -s "$2" shell mkdir -p ${device_result_dir}
}

benchmark_test() {
  echo "sh ${device_script_dir}/device_benchmark_test.sh $test_info"
  adb -s "$1" shell "sh ${device_script_dir}/device_benchmark_test.sh $test_info"
}
config() {
  echo $1
  adb -s "$2" shell "sed -i \"s/\$(grep NODE_ARGS /etc/init/system.liauto.mesh_bases@1.0-service.rc)/export NODE_ARGS ${1}/\" /etc/init/system.liauto.mesh_bases@1.0-service.rc;grep NODE_ARGS /etc/init/system.liauto.mesh_bases@1.0-service.rc"
}
stop() {
  adb -s "$1" shell "ps -ef | grep logcat | cut -c 9-18  | xargs kill -9"
  adb -s "$1" shell "ps -ef | grep sysinfo_hu_collector.js | cut -c 9-18  | xargs kill -9"
}
main() {

  local action=$1
  local target=$2

  if [ "$target" == "" ]; then
    echo "lose a params,must have devicesId,please check your script!"
    return
  fi

  shift
  if [ "$action" == "device_init" ]; then
    init_params
    device_init "$target"
  elif [ "$action" == "clean" ]; then
    init_params
    clean "$target"
  elif [ "$action" == "push" ]; then
    init_params
    push "$target"
  elif [ "$action" == "build" ]; then
    build
    init_params
    push "$target"
  elif [ "$action" == "pull_result" ]; then
    init_params
    benchmark_init "$@" "$target"
    pull_result "$target"
  elif [ "$action" == "test" ]; then
    init_params
    benchmark_init "$@" "$target"
    benchmark_test "$target"
    pull_result "$target"
  elif [ "$action" == "config" ]; then
    config "$@" "$target"
  elif [ "$action" == "stop" ]; then
    init_params
    stop "$target"
  elif [ "$action" == "pick" ]; then
    init_params
    device_init "$target"
    clean "$target"
    push "$target"
    benchmark_init "$@" "$target"
    benchmark_test "$target"
    # pull_result
  elif [ "$action" == "all" ]; then
      echo "sorry , no support!,please use [pick devicesId]]"
  fi
}

main "$@"
#./iot_benchmark.sh all TEST_SERVER=veh-downloader TEST_TIME=100 TEST_QPS=1 TEST_TIMEOUT=1 MAX_TIMES=1